package com.bnz.order.controller;

import com.bnz.common.entity.TbPayLog;
import com.bnz.order.entity.Cart;
import com.bnz.order.entity.TbAddress;
import com.bnz.order.entity.TbOrder;
import com.bnz.order.service.AddressService;
import com.bnz.order.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private AddressService addressService;
    // 1. 展示订单页面
    @GetMapping({"/","/getOrderInfo.html"})
    public String order(Model model, HttpSession session){
        // 1.1 获取用户登录名
        String name = (String) session.getAttribute("name");
        // 1.2 根据登录名得到收件人的地址信息信息
        List<TbAddress> addressList = addressService.findAddressList(name);
        // 1.3 根据登录名得到购物车列表
        List<Cart> cartList = orderService.findOrderList(name);
        // 1.4 将得到的购物车列表，收货人地址信息列表放到model中
        model.addAttribute("addressList",addressList);
        model.addAttribute("cartList",cartList);
        return "getOrderInfo";
    }
    // 2. 提交订单到支付页面
    @PostMapping("/order/add")
    public String orderAdd(HttpSession session,TbOrder tbOrder,Model model){
        // 2.1 得到用户登录名
        String name = (String) session.getAttribute("name");
        // 2.2 添加订单
        orderService.add(tbOrder,name);
        // 2.3 从redis中得到支付日志
        TbPayLog payLog = orderService.getPaylogByRedis(name);
        // 2.4 将支付日志放到model中
        model.addAttribute("paylog",payLog);
        return "pay";
    }
}
